import 'package:flutter/material.dart';
import 'package:test/homePage.dart';
import 'package:test/channel.dart';
import 'package:test/platformdemo/platform.dart';
import 'package:test/path_provider/path_provider.dart';
// import 'home.dart';
// import 'mine.dart';
// import 'category.dart';
// import 'baoCircle.dart';

class Taps extends StatefulWidget {
  int tabIndex = 0;
  // Taps({Key? key, this.tabIndex = 0}) : super(key: key);
  Taps({Key? key}) : super(key: key);

  @override
  _TapsState createState() => _TapsState();
}

class _TapsState extends State<Taps> {
  List pageList = [
    MyHomePage(title: 'Flutter Demo Home Page1'),
    ChannelPage(title: 'Flutter Demo Home Page2'), // const CategoryPage(),
    CustomExample(),
    PathProviderHome(title: 'Flutter Demo Home Page4'),
  ];
  // int currentIndex = widget.tabIndex; //再此声明获取不到widget,要放在initState()函数中
  // int _currentIndex = 0;
  var tabImages; //选项卡选项图片
  final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();

  List list = ['第一条', '第二条'];
  @override
  void initState() {
    // _currentIndex = widget.tabIndex;
    super.initState();

    tabImages = [
      //选项卡选项图片
      [getTabImage('assets/shanchu_1.png'), getTabImage('assets/shanchu.png')],
      [getTabImage('assets/shanchu_1.png'), getTabImage('assets/shanchu.png')],
      [getTabImage('assets/shanchu_1.png'), getTabImage('assets/shanchu.png')],
      [getTabImage('assets/shanchu_1.png'), getTabImage('assets/shanchu.png')]
    ];

    _loadLastTabIndex();


  }


  Image getTabImage(path) {
    return Image.asset(
      path,
      width: 20.0,
      height: 20.0
    );
  }


  void _loadLastTabIndex() async {
  }

  void _saveTabIndex(int index) async {
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // appBar: _appBar(),
        body: _body(),
        bottomNavigationBar: _bottomNavigationBar());
  }

  AppBar _appBar() {
    return AppBar(
      title: const Text('welcome'),
      actions: [IconButton(onPressed: () {}, icon: const Icon(Icons.search))],
    );
  }

  Widget _body() {

    return this.pageList[widget.tabIndex];
  }

  Widget getTabIcon(int curIndex) {
    //设置tabbar选中和未选中的状态图标
    if (curIndex == widget.tabIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }

  Widget _bottomNavigationBar() {
    return BottomNavigationBar(
      currentIndex: widget.tabIndex,
      onTap: (int index) {
        // print(index);
        setState(() {
          widget.tabIndex = index;
        });
        _saveTabIndex(index);
      },
      items: [
        BottomNavigationBarItem(
          // title: Text("首页"),
          label: "首页",
          icon: getTabIcon(0),
          // icon:Icon(Icons.home),
        ),
        BottomNavigationBarItem(
          // title: Text("保圈"),
          label: "购物车",
          icon: getTabIcon(1),
          // icon:Icon(Icons.home),
        ),
        BottomNavigationBarItem(
          // title: Text("分类"),
          label: "分类",
          icon: getTabIcon(2),
          //  icon:Icon(Icons.search),
        ),
        BottomNavigationBarItem(
          // title: Text("我的"),
          label: "我的",
          icon: getTabIcon(3),
        )
      ],
      iconSize: 36.0,
      fixedColor: Colors.red,
      type: BottomNavigationBarType
          .fixed, //设置底部tabs有很多个按钮时，可以都显示，并根据屏宽自适应大小，fixed 固定大小和无动画， shifting 大小不固定、选中有动画
    );
  }
}
